$(function(){ 
	var detailId=queryString('updateId');
	getInformationFlow(detailId);
	requestDetail(detailId)
	requestSort();
	requestClassification();
	requestSource();
	requestPort();
	var flag=0;
	var taskFinshFlag=0;
	var file={};
	var sorFlag=false;
	var checkFlag={sele:false,pes:false,sort:false};
	//确认修改按钮
	$("#requestUpdate").click(function(){
		myTool.alert({		
			title:"提示",
			context:"是否确认点评?",
			isMask:true,
			callback:function(){
				inputCondition();
			}
		});

	})
	$("#letgoback").click(function(){
		  window.history.back();
	});
	function inputCondition(){
		 var id=detailId;
		 var qualityLvId=$("input[name='infoLv']:checked").val();
		 var qualityLvName=$("input[name='infoLv']:checked").next().html();

		 if(qualityLvId=="" || qualityLvId==undefined)
		 {
		 	myTool.alert({		
				title:"提示",
				context:"请选择录入点评",
				isMask:true,
				callback:function(){}
			});
		 	 return false;
		 }
		 var taskLv=$("input[name='taskLv']:checked").val();
		 var taskLvName=$("input[name='taskLv']:checked").next().html();
		
		 if(taskFinshFlag==1)
		 {
		 	if(taskLv==undefined)
			{
		 		
		 		myTool.alert({		
				title:"提示",
				context:"请选择完成情况点评",
				isMask:true,
				callback:function(){}
				});
		 	 	return false;
		 	}
		 	var  completeText =$("#complete-text").val();
			 if(completeText==$("#complete-text").attr("placeholder") || completeText=="")
			 {
			 	 myTool.alert({		
					title:"提示",
					context:"请输入完成情况意见!",
					isMask:true,
					callback:function(){}
				});
			 	 return false;
			 }
		 }
		 else
		 {
		 	taskLvName="0";
		 		taskLv="0";
		 }

		 
		 var text=$("#commit-text").val();
		 if(text==$("#commit-text").attr("placeholder") || text=="")
		 {
		 	 myTool.alert({		
				title:"提示",
				context:"请输入录入和收集意见!",
				isMask:true,
				callback:function(){}
			});
		 	 return false;
		 }
		 YuXi.util.post("CommentFilter/getInfoTogether.do",{informationId:id,text:text,qualityLvId:qualityLvId,taskLvId:taskLv,qualityLvName:qualityLvName,taskLvName:taskLvName,completeText:completeText },function(data){
			  $("#modal-input-task-finsh-context").modal("hide");	 
			  myTool.alert({
				 title:"提示",
				 context:data.msg
			 });
			 if(data.status){
					window.location.href = 'stayComment.jsp?menuId=19';
			 }
			  
		  },function(){
			  myTool.alert({
					 title:"提示",
					 context:"未知错误"
				 });
		  });
		  
	}
	
	
	function getInformationFlow(id){
		var url="getInformationFlowByInfoId.do";
		var infoId={infoId:id};
		var object={};
		YuXi.util.get(url,infoId,function(data){
			if(data.status){
				object=data.object;
				var flow;
				
				for (var i = 0; i < object.length; i++) {
					if(object[i].lastFlowId==1){
						flow="信息管理";
					}
					if(object[i].lastFlowId==3){
						flow="复核";
					}
					if(object[i].lastFlowId==2){
						flow="初审";
					}
					if(object[i].lastFlowId==4){
						flow="审核";
					}
					var time = YuXi.getTime.get(object[i].createTime);
					
					if(object[i].remark==undefined){
						object[i].remark="";
					}
					var html = "<tr ><td style='border:none;'>" + object[i].userName + "</td>" +
					
					"<td style='border:none;'>" +time  + "</td>" +
					"<td style='border:none;'>" + object[i].operation + "</td>" +
					"<td style='border:none;'>" + object[i].remark + "</td>" +
					
					"<td style='border:none;'>" +flow  + "</td>" +
 					"</tr>";
					$("#tbody").append(html);
				}
			}
		},function(){
			
		});
	}

	
//正则取链接传来的数据
function queryString(key)
{
    var regex_str = "^.+\\?.*?\\b"+ key +"=(.*?)(?:(?=&)|$|#)"
    var regex = new RegExp(regex_str,"i");
    var url = window.location.toString();
    if(regex.test(url)) return RegExp.$1;
    return undefined;
}



//向后台请求渠道信息
	//所有数据，和上一层id
	var childer2=[];  
	var childer=[];  
	function xxx(object,className){
		var type="";
		var name="";
		var flag="";
		if(className=="#treeTable1")
		{
			type="checkbox";
			name="sourceCheck";
			flag="name";
		}
		else
		{
			type="radio";
			name="safePort";
			flag="portName";
		}
		var a=[];
		var flag=0;
			var html="";
		var flaga={};
		
		function treeMenu(a){
		    this.tree=a||[];
		    this.groups={};
		};
		treeMenu.prototype={
		    init:function(superId){
		  
		        this.group();
		        return this.getDom(this.groups[superId]);
		    },
		    group:function(){
		    
		        for(var i=0;i<this.tree.length;i++){
		            if(this.groups[this.tree[i].superId]){
		                this.groups[this.tree[i].superId].push(this.tree[i]);
		            }else{
		                this.groups[this.tree[i].superId]=[];
		                this.groups[this.tree[i].superId].push(this.tree[i]);
		            }
		        }

		    },

		    getDom:function(a){
		    	
		    	
		        if(!a){return ''}
		       flag=0;
		        var html="";
		    
		    	if(a[0].superId==0)
		    	{
		    			/*$scope.sorce=a;//第一层结构
		    			//console.log($scope.sorce);*/
		    			for(var i=0;i<a.length;i++)
		    			{
		    				
		    				if(className=="#treeTable2")
		    				{
		    					html='<tr isChange=false id="'+a[i].id+'"   hasChild="hasChild" data-superid="'+a[i].superId+'" style="color:black;" ><td class="bianhao">'+(i+1)+'</td><td>'+a[i].name+'</td><td><input data-name='+a[i].portName+' data-id='+a[i].id+' type='+type+' name='+name+'></td></tr>\n';		
		    				}
		    				else
		    				{
		    					html='<tr isChange=false id="'+a[i].id+'"   hasChild="hasChild" data-superid="'+a[i].superId+'" style="color:black;" ><td class="bianhao">'+(i+1)+'</td><td>'+a[i].name+'</td><td><input data-name='+a[i].name+' data-id='+a[i].id+' type='+type+' name='+name+'></td></tr>\n';		

		    				}
		    				$(className).append(html);
		    				
		    			}
		    			
		    			flag=1;
		    	}
		    	////console.log("aa"+JSON.stringify(a));
		    	if(flag==0)
		    	{
		    			var length=a.length;
		    			var superid=0;
		    			for(var i=0;i<length;i++)
		    			{	
		    				superid=a[i].superId;
		    				if(isChilde(a[i].id,object))
		    				{
		    					
		    					if(className=="#treeTable2")
		    					{

		    					html+='<tr isChange=false id="'+a[i].id+'" hasChild="hasChild" pId="'+a[i].superId+'"   data-superid="'+a[i].superId+'" style="color:black;" ><td class="bianhao">'+(i+1)+'</td><td>'+a[i].name+'</td><td><input data-name='+a[i].portName+' data-id='+a[i].id+' type='+type+' name='+name+'></td> </tr>\n';		
		    					}
		    					else
		    					{
		    						html+='<tr isChange=false id="'+a[i].id+'" hasChild="hasChild" pId="'+a[i].superId+'"   data-superid="'+a[i].superId+'" style="color:black;" ><td class="bianhao">'+(i+1)+'</td><td>'+a[i].name+'</td><td><input data-name='+a[i].name+' data-id='+a[i].id+' type='+type+' name='+name+'></td> </tr>\n';		
		    					}

		    				}  
		    				else
		    				{  			
		    					if(className=="#treeTable2")
		    					{
		    							html+='<tr isChange=false id="'+a[i].id+'" pId="'+a[i].superId+'"   data-superid="'+a[i].superId+'" style="color:black;" ><td class="bianhao">'+(i+1)+'</td><td>'+a[i].portName+'</td><td><input data-name='+a[i].name+' data-id='+a[i].id+' type='+type+' name='+name+'></td></tr>\n';		
		    				
		    					}
		    					else
		    					{
		    						html+='<tr isChange=false id="'+a[i].id+'" pId="'+a[i].superId+'"   data-superid="'+a[i].superId+'" style="color:black;" ><td class="bianhao">'+(i+1)+'</td><td>'+a[i].name+'</td><td><input data-name='+a[i].name+' data-id='+a[i].id+' type='+type+' name='+name+'></td></tr>\n';		
		    				
		    					}
		    				}
		    				
		    				
		    				    
		    			}
		    				if(className=="#treeTable1")
		    				{
		    					childer.push({superid:superid,html:html});
		    				}
		    				else
		    				{
		    					childer2.push({superid:superid,html:html});
		    				}

		    			
		    		
		    		
		    			
		    	}
		    	 
		        for(var i=0;i<a.length;i++)
		        {
		           this.getDom(this.groups[a[i].id]);

		        }

	        /*html+='</optgroup>\n';*/

	       /* $("#"+id).append(html);*/
	        return html;
	    }
	   

	}
 var html=new treeMenu(object).init(0);
option(className);
	sorFlag=true;
}
	



//判断是否有子节点
function isChilde(id,object)
{
	for(var i=0;i<object.length;i++)
	{
		if(object[i].superId==id)
		{
			return true;
		}
	}
	return false;
}
	function option(className)
	{			
			var option = {
	                theme:'vsStyle',
	              
	                beforeExpand : function($treeTable, id) {

	            	var html="";
	                    //判断id是否已经有了孩子节点，如果有了就不再加载，这样就可以起到缓存的作用
	                    if ($('.' + id, $treeTable).length) { return; }
	            
	                   var index=$(className).find("tr").eq(id).attr("id");
	                   if(className=="#treeTable2")
		    			{
		                   for(var i=0;i<childer2.length;i++)
		                   {
		                   	if(id==childer2[i].superid)
		                   	{
		                   		html=childer2[i].html;
		                   	}
		                   }
		                 }
		                 else
		                 {
		                 	 for(var i=0;i<childer.length;i++)
		                   	{
		                   		if(id==childer[i].superid)
		                   		{
		                   			html=childer[i].html;
		                   		}
		                  	 }
		                 }
	                
	                     $treeTable.addChilds(html);
	                       $("input[name='sourceCheck']").each(function(){
					
							for(var i=0;i<Allsourcename.length;i++){
								
								if($(this).attr("data-id")==Allsourcename[i]){
									
									$(this).attr("checked","checked");
									break;
								}
							}
							
						});
					},
					 onSelect : function($treeTable, id) {
	                    window.console && console.log('onSelect:' + id);
	                    
	                }


		}	
	
		$(className).treeTable(option);
	}


var x=1;	//向后台请求渠道信息
function space(superid,object)
{

	if(superid==0)
	{

		return 0;
	}
	for(var i=0;i<object.length;i++)
	{
		if(object[i].id==superid)
		{
			
			x=space(object[i].superId,object);
			
		}
	}
	return x+1;

}


function requestSource()
{	


	var url="getAllInfomationSource.do";//渠道地址
	var data={};
	var object={};
	var superId1=[];//第一层id
	YuXi.util.get(url,"null",function(data){
		object=data.object;
		
	xxx(object,"#treeTable1");
	
		
	},function(){});//请求数据



}


//根据id向后台请求数据详情
function requestDetail(id)
{	
	var url="getUserInformationById.do";//向后台请求数据id
	var object={}
	var sourceName;
	var personId;
	var personName;
	var dataid={informationId:id};
	YuXi.util.get(url,dataid,function(data){
		object=data.object;
		sourceName=object.informationSourceNames.split(",");
		personName=object.inputPerSonNames.split(",");
		personId=object.inputPersonID.split(",");
//		//console.log("数据请求成功"+JSON.stringify(object));
			Allsourcename=object.informationSourceID.split(",");;
		
			object.informationSourceNames=object.informationSourceNames.substring(0,object.informationSourceNames.length-1);
		YuXi.fenkouSourceUpdate.get(object.informationSourceID,object.safetyPortId);
		
		addSelect(".sele",object.safetyPortNames);
		addSelect(".pes",object.informationClassificationNames);
		addSelect(".sort",object.informationSortNames);
		for(var i=0;i<personName.length;i++){
			if(personName[i]!=""){
				
				/*if(i<2){*/
					var html="<span class='parsbox'><span  class='glyphicon glyphicon-user' aria-hidden='true' ></span><span class='select-data-user' data-id="+personId[i]+">"+personName[i]+"</span>&nbsp;&nbsp;<span   class='glyphicon glyphicon-remove' aria-hidden='true'></span></span>"
			        $(".label1").append(html);
			/*	}else
				{
					html="<span style='display:none' class='parsbox'><span  class='glyphicon glyphicon-user' aria-hidden='true' ></span><span class='select-data-user' data-id="+personId[i]+">"+personName[i]+"</span>&nbsp;&nbsp;<span   class='glyphicon glyphicon-remove' aria-hidden='true'></span></span>"
			        $(".label1").append(html);
					
				}
				 if(i==2){
					html="  ...";
					$(".label1").append(html);
				 }*/
				html= '<li><input type="checkbox"  data-id='+personId[i]+' value='+personName[i]+' name="Check">'+personName[i]+'</li>';
				$("#addRequest").find("ul").append(html);
			}
			
		}

		$(".briefing").text(object.informationProfile);//简介
		$(".basic").text(object.standard);//标准
		$(".step").text(object.mesure);//措施
		$(".cont").text(object.informationConternt);//内容
		if(object.flowId>=9 && object.flowId!=11){
			$("#taskFinsh-container").show();
			$(".complete-div").show();
			taskFinshFlag=1;
		}
		file=object.appendix;
		if(file==null||file=="[]"){
			
		}else{
			file=JSON.parse(file);
			var appendix=object.appendix.substring(1,object.appendix.length-1);
			appendix=appendix.split(",");
			YuXi.appendix.get(appendix);
		}
	},function(){
		//console.log("数据请求失败");

	})

}


var fff={sele:true,sort:false,pes:false};
function addSelect(className,value){
	var length=className.length;
	
	var newname=className.substring(1,length);
	var time=setInterval(function(){
		if(fff.sele==true && fff.sort==true && fff.pes==true)
		{

			//clearInterval(time);
		}
		if(checkFlag[newname]==true)
		{
			$(className+" option").each(function(){
			if($(this).html()==value){
				$(this).attr("selected","selected");

				 if(className==".pes")
				{
					$(".select2-selection__rendered").eq(2).attr("title",value);
					$(".select2-selection__rendered").eq(2).html(value);
				}
				else
				{
					$(".select2-selection__rendered").eq(3).attr("title",value);
					$(".select2-selection__rendered").eq(3).html(value);
				}
				fff[newname]=true;
			}
			});
		}
		
	},50);
	
}	





/*判断输入合法性*/
function isNull(str){
	if(str==null||str==""){
		return false;
	}else{
		return true;
	}
}


function modal(msg,cont){
	myTool.alert({		
		title:msg,
		context:cont,
		isMask:true,
		callback:function(){}
	});
}

YuXi.util.get("getAllInformationQuality.do",null,function(data){
	 if(data.status){
		 var obj=data.object;
		 for(var i=0;i<obj.length;i++){				
			 $("#infoAndS-container").append("<input type='radio' name='infoLv' value='"+obj[i].id+"' ><span>"+obj[i].name+"</span></input>");
		 }
		 
	 }
});

YuXi.util.get("getAllTasCompletion.do",null,function(data){
	 if(data.status){
		 var obj=data.object;
		 for(var i=0;i<obj.length;i++){				
			 $("#taskFinsh-container").append("<input type='radio' name='taskLv' value='"+obj[i].id+"' > <span>"+obj[i].name+" </span></input>");
		 }
		 
	 }
});

//向后台请求分级信息
function requestClassification()
{	
	var url="getAllInformationClassification.do";//分级地址
	var data={};
	var object={};
	data=YuXi.util.get(url,"null",function(data){
		object=data.object;
		//用data循环添加分口信息
		for(var i=0;i<object.length;i++)
		{
			var html="<option  value="+object[i].id+" >"+object[i].name+"</option>";
			$(".pes").append(html);
		}
		checkFlag.pes=true;
		
	},function(){
		
	});//请求数据
	
}

//向后台请求分类信息
function requestSort()
{	//数据模拟
	var url="getAllInfomationSort.do";//分类地址
	var data={};
	var object={};
	data=YuXi.util.get(url,"null",function(data){
		object=data.object;
		//用data循环添加分类信息
		for(var i=0;i<object.length;i++)
		{
			var html="<option  value="+object[i].id+" >"+object[i].name+"</option>";
			$(".sort").append(html);
		}
		checkFlag.sort=true;
	},function(){
		
	});//请求数据
	
}
//查询向后台请求分口信息
	function requestPort()
	{	var url="getAllInformationPort.do";//分口地址
		var data={};
		var object={};
		data=YuXi.util.get(url,null,function(data){
			object=data.object;
			//用data循环添加分口信息	
			for(var i=0;i<object.length;i++)
			{
				 var html="<option  value="+object[i].id+" >"+object[i].portName+"</option>";
				 $(".rePort").append(html);
			}
			checkFlag.sele=true;
		},function(){});//请求数据
		
	}

})